package xps.and.uudaijia.userclient.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;

import java.util.List;

import rx.Subscriber;
import xps.and.uudaijia.userclient.adapter.CapitalAdapterDaijia;
import xps.and.uudaijia.userclient.data.baen.CapitalListRes;
import xps.and.uudaijia.userclient.data.net.UserNetWorks;
import xps.and.uudaijia.userclient.util.ListDividerItemDecoration;
import xps.and.uudaijia.userclient.view.base.BaseSwipeRefreshFragment;

public class CapitalFragment extends BaseSwipeRefreshFragment {
    String userId;
    String startDate = "";
    String endDate = "";

    @Override
    protected void getData(int pageIndex) {
        int page = pageIndex + 1;
        doLoadMore(page);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        doLoadMore(1);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        ListDividerItemDecoration list = new ListDividerItemDecoration((int) (1 * getActivity().getResources().getDisplayMetrics().density));
        recyclerView.addItemDecoration(list);
        setRefreshEnable(false);
        abstractRecyclerAdapter = new CapitalAdapterDaijia(activity, recyclerView);
        abstractRecyclerAdapter.setAutoLoadMoreEnable(true);
        abstractRecyclerAdapter.setOnItemClickListener((v, p) -> {
        });
        super.onActivityCreated(savedInstanceState);
        doMore();
    }

    void doMore() {
        UserNetWorks.getCapitalList("10", "1", new Subscriber<CapitalListRes>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();

            }

            @Override
            public void onNext(CapitalListRes capitalListRes) {
                List<CapitalListRes.ReturnBodyBean> list = capitalListRes.getReturn_body();

                if (list != null && list.size() <= 0) {
                    onFail("没有更多");
                    return;
                }
                adapterListData = list;
                onResponce();
            }
        });

    }

    public void doLoadMore(final int pageIndex) {

        UserNetWorks.getCapitalList("10", "1", new Subscriber<CapitalListRes>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();

            }

            @Override
            public void onNext(CapitalListRes capitalListRes) {
                List<CapitalListRes.ReturnBodyBean> list = capitalListRes.getReturn_body();

                if (list != null && list.size() <= 0) {
                    onFail("没有更多");
                    return;
                }
                adapterListData = list;
                onResponce();
            }
        });

    }

}
